#!/usr/bin/sh

# Test for temporal algebra in LatLon location.
rm /tmp/rinput1_test.txt
rm /tmp/rinput2_test.txt
rm /tmp/rinput3_test.txt
rm /tmp/rinput4_test.txt

# Create random area test maps.
for i in {1..60}
  do
    if [[ "$i" -le 20 ]]; then
      echo rtestpoint1_$i >> /tmp/rinput1_test.txt
      r.mapcalc expr="rtestpoint1_$i = 1"
    elif [ "$i" -gt 20 ] && [ "$i" -le 40 ]; then
      echo rtestpoint2_$i >> /tmp/rinput2_test.txt
      r.mapcalc expr="rtestpoint2_$i = 2"
    else
      echo rtestpoint3_$i >> /tmp/rinput3_test.txt
      echo rtestpoint4_$i >> /tmp/rinput4_test.txt
      r.mapcalc expr="rtestpoint3_$i = 3"
      r.mapcalc expr="rtestpoint4_$i = 4"
    fi
  done

# Create STVDS and register test maps.
t.create --o output=rtest1 type=strds title="test dataset" descr="test dataset"
t.create --o output=rtest2 type=strds title="test dataset" descr="test dataset"
t.create --o output=rtest3 type=strds title="test dataset" descr="test dataset"
t.create --o output=rtest4 type=strds title="test dataset" descr="test dataset"

t.register -i --o type=rast input=rtest1 file=/tmp/rinput1_test.txt increment="1 days" start="2013-01-01"
t.register -i --o type=rast input=rtest2 file=/tmp/rinput2_test.txt increment="1 days" start="2013-01-10"
t.register -i --o type=rast input=rtest3 file=/tmp/rinput3_test.txt increment="3 days" start="2013-01-01"
t.register -i --o type=rast input=rtest4 file=/tmp/rinput4_test.txt increment="3 days" start="2013-01-10"

t.merge --o inputs=rtest2,rtest3 output=rtest5 type=strds
t.merge --o inputs=rtest1,rtest2 output=rtest6 type=strds

# Test different options.
python t.rast.mapcalc.py --o expression='R = rtest1 {during,+} rtest2 {during,|*} rtest4' basename="rmap"
t.rast.list input=R column=name,start_time,end_time
python t.rast.mapcalc.py --o expression='R = int(rtest1) + tan(rtest2)' basename="rmap"
t.rast.list input=R column=name,start_time,end_time
python t.rast.mapcalc.py --o expression='R = if(rtest1 == 1, rtest6)' basename="rmap"
python t.rast.mapcalc.py --o expression='R = if(isnull(rtest1), rtest2)' basename="rmap"
python t.rast.mapcalc.py --o expression='R = if({equal},rtest1 == 1, rtest6)' basename="rmap"
python t.rast.mapcalc.py --o expression='R = if(rtest1 == 1 || rtest2 == 2, rtest6)' basename="rmap"
python t.rast.mapcalc.py --o expression='R = if(rtest6 == 1, rtest1,rtest2)' basename="rmap"
python t.rast.mapcalc.py --o expression='R = if(rtest1 == 1, rtest2,null())' basename="rmap"
python t.rast.mapcalc.py --o expression='R = if({during},rtest1 == 1, 10, rtest3)' basename="rmap"
python t.rast.mapcalc.py --o expression='R = if({equal},rtest6 == 1, rtest1,rtest2)' basename="rmap"
python t.rast.mapcalc.py --o expression='R = if(start_date() > "2013-01-15" && rtest1 == 1, rtest2)' basename="rmap"
python t.rast.mapcalc.py --o expression='R = if(exist(rtest1), 1)' basename="rmap"
python t.rast.mapcalc.py --o expression='R = if(start_date() > "2013-01-15" && rtest1 == 1 || start_day() < 18, rtest2)' basename="rmap"
python t.rast.mapcalc.py --o expression='R = if({during},start_date() > "2013-01-15" && rtest1 == 1 || start_day() < 18, rtest3)' basename="rmap"
python t.rast.mapcalc.py --o expression='R = if(start_date() > "2013-01-15" && rtest1 == 1 && start_day() < 18 && rtest2 < 3, 1)' basename="rmap"
python t.rast.mapcalc.py --o expression='R = if(start_date() > "2013-01-15" && rtest1 == 1 || start_day() < 18, rtest1, rtest2)' basename="rmap"
python t.rast.mapcalc.py --o expression='R = if(start_date() > "2013-01-15" && rtest1 == 1 || start_day() < 18, 1, null())' basename="rmap"
python t.rast.mapcalc.py --o expression='R = if(start_date() > "2013-01-15" && rtest1 == 1 && start_day() < 18, rtest2, null())' basename="rmap"
python t.rast.mapcalc.py --o expression='R = if({during},start_date() > "2013-01-15" && rtest1 == 1 && start_day() < 18, rtest3, null())' basename="rmap"

# Neighbourhood analysis.
python t.rast.mapcalc.py --o expression='R = rtest1[1]' basename="rmap"

